<template>
  <div id="popupbody" v-if="dialogVisible">
    <div class="popupbody-box">
      <div class="popupbody-title">
        <span>选择识别器</span>
        <span class="el-icon-close" @click="closePOpup"></span>
      </div>
      <div class="popupbody-boxin" style="padding:10px;">
        <!--      搜索组件-->
        <SearchModle dataKey="getDeviceModle" @searchFn="searchFn"></SearchModle>
        <!--      end-->
        <el-table :data="arryList"
                  height="400"
                  :stripe="tableStyle.tableStripe"
                  :border="tableStyle.tableBorder"
                  tooltip-effect="dark"
                  @row-click="rowClick"
                  @select="selectFn"
                  ref="multipleTable">
          <el-table-column
              type="selection"
              width="50"
              align="center">
          </el-table-column>
          <el-table-column
              prop="id_code"
              label="识别号"
              align="center">
          </el-table-column>
          <el-table-column
              label="识别范围"
              align="center">
            <template slot-scope="scope">
              {{ minrssi[scope.row.scope] }}米
            </template>
          </el-table-column>
          <el-table-column
              label="绑定区角"
              align="center">
            <template slot-scope="scope">
              {{ scope.row.corner ? scope.row.corner.name : '' }} <span class="clearCornerbanding"
                                                                        v-if="scope.row.corner"
                                                                        @click="UnbundlingFn(scope.$index)">【解绑】</span>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            background
            layout="prev, pager, next"
            :current-page="page.pageindex"
            :total="page.total"
            :page-size="page.pagesize"
            @current-change="handleSizeChange">
        </el-pagination>
      </div>
      <div class="popupbody-footer">
        <el-button size="small" @click="closePOpup">返回</el-button>
        <el-button type="primary" size="small" @click="submitFormAdd()">确认并保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {pagination, tableoption} from '@/utils/util.js'

export default {
  name: 'deviceMultiple',
  props: ['visible', 'dynamicTagsArray'],
  data() {
    return {
      dialogVisible: this.visible,
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      arryList: [],
      SelectedQueue: {},
      // 搜索
      searchForm: {},
      minrssi: {
        "-127": 0,
        "-51": 0.5,
        "-60": 1,
        "-63": 2,
        "-66": 3,
        "-68": 4,
        "-70": 5,
        "-72": 6,
        "-74": 7,
        "-76": 8,
        "-78": 9,
        "-80": 10,
        "-82": 11,
        "-84": 12,
        "-86": 13,
        "-88": 14,
        "-90": 15
      }
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      console.log('this.visible:', this.visible)
      if (this.visible) {
        console.log('数据清理选中:', this.dynamicTagsArray)
        this.SelectedQueue = this.getJson(this.dynamicTagsArray)
        this.InitFn()
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化
    async InitFn() {
      let n = this;
      this.searchForm.p = this.page.pageindex
      let data = await this.$fetch(this.api.admin_facility_index, this.searchForm)
      if(!data.code){
        this.arryList = data.list.data;
        this.page.total = data.list.total;
        this.$nextTick(function () {
          n.checked() // 每次更新了数据，触发这个函数即可。
        })
      }

    },
    // 搜索
    searchFn(option) {
      this.page.pageindex = 1
      this.searchForm = option
      this.InitFn()
    },

    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 提交数据
    async submitFormAdd() {
      console.log('applyBookBox提交:', this.getJson(this.SelectedQueue))
      if (JSON.stringify(this.SelectedQueue) === '{}') {
        this.$message.error('选择设备号')
      } else {
        for (let i in this.SelectedQueue) {
          if (this.SelectedQueue[i].corner) {
            this.$message.error('设备中已绑定对应区角，请先解绑设备区角')
            return
          }
        }
        this.$emit('getDeviceFn', this.getJson(this.SelectedQueue))
        this.closePOpup();
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 设置默认选中
    checked() {
      console.log('选中的队列：', this.SelectedQueue)
      for (let i = 0; i < this.arryList.length; i++) {
        console.log('this.SelectedQueue[this.arryList[i].id]:',this.SelectedQueue[this.arryList[i].id])
        console.log('this.SelectedQueue[this.arryList[i].id]:',this.arryList[i].id)
        if (this.SelectedQueue[this.arryList[i].id]) {
          this.$refs.multipleTable.toggleRowSelection(this.arryList[i], true)
        } else {
          this.$refs.multipleTable.toggleRowSelection(this.arryList[i], false)
        }
      }
    },
    // 行选中
    rowClick(row, column, event) {
      if(column.label!=='绑定区角'){
        if (row.corner) {
          this.$message.error('该设备已绑定对应区角，请先解绑设备区角')
          return
        }
        for (let i in this.arryList) {
          if (this.arryList[i].id === row.id) {
            if (!this.SelectedQueue[row.id]) {
              this.SelectedQueue[row.id] = row
              this.$refs.multipleTable.toggleRowSelection(this.arryList[i], true)
            } else {
              delete this.SelectedQueue[row.id]
              this.$refs.multipleTable.toggleRowSelection(this.arryList[i], false)
            }
          }
        }
      }
    },
    // 单选全选内容
    selectFn(option){
      // 当前页的数据内容
      let a_id = [];
      for (let i in option) {
        a_id.push(option[i].id)
      }
      for (let n in this.arryList) {
        if (option.length) {
          if (a_id.indexOf(this.arryList[n].id) === -1) {
            delete this.SelectedQueue[this.arryList[n].id]
          } else {
            this.SelectedQueue[this.arryList[n].id] = this.arryList[n]
          }
        } else {
          if (this.SelectedQueue[this.arryList[n].id]) {
            delete this.SelectedQueue[this.arryList[n].id]
          }
        }
      }
    },
    // 解绑了
    UnbundlingFn(index) {
      this.$confirm('解绑'+this.arryList[index].corner.name+'该区域的设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(index)
      }).catch(() => {
      })
    },
    async detelRespon(index) {
      let n = this;
      this.searchForm.p = this.page.pageindex
      let data = await this.$post(this.api.corner.device.Unbundling, {id: this.arryList[index].id})
      if (!data.code) {
        n.InitFn()
        n.$message.success(data.msg)
      } else {
      }
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
.popupbody-boxin {
  min-width: 780px;
}

.clearCornerbanding {
  color: red;
}

.clearCornerbanding:hover {
  text-decoration: underline;
}
</style>
